package com.bawei.splash


import ImageBannerAdapter
import android.content.Intent
import android.os.CountDownTimer
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.RoutePath
import com.bawei.common.base.BaseActivity
import com.bawei.common.entity.BannerEntity
import com.bawei.main.MainActivity
import com.bawei.splash.databinding.ActivitySplashBinding
import com.gyf.immersionbar.ImmersionBar
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer


class SplashActivity : BaseActivity<ActivitySplashBinding>() {

    private lateinit var mImgAdapter: ImageBannerAdapter
    private var bannerData = mutableListOf<BannerEntity>()
    private lateinit var mCountDownTimer: CountDownTimer

    override fun getLayoutId(): Int = R.layout.activity_splash

    override fun initViews() {
        //沉浸式状态栏
        ImmersionBar.with(this).init()

        //判断是否是第一启动
        var isFirstBoot = MMKV.defaultMMKV().decodeBool("isFirstBoot", true)
        if (isFirstBoot){
            MMKV.defaultMMKV().encode("isFirstBoot", false)
            //显示引导页
            mDataBinding.clSplashApp.visibility = View.VISIBLE

            bannerData.add(BannerEntity(R.drawable.app_img_0, ""))
            bannerData.add(BannerEntity(R.drawable.app_img_1, ""))
            bannerData.add(BannerEntity(R.drawable.app_img_2, ""))

            mImgAdapter = ImageBannerAdapter(bannerData)
            mDataBinding.appBanner.setAdapter(mImgAdapter)

            //与当前Activity的生命周期关联
            mDataBinding.appBanner.addBannerLifecycleObserver(this)
            //设置指示器的样式
            mDataBinding.appBanner.indicator = CircleIndicator(this)
            //设置切换过度效果
            mDataBinding.appBanner.addPageTransformer(AlphaPageTransformer())

            //设置切换事件监听
            mDataBinding.appBanner.addOnPageChangeListener(object : OnPageChangeListener {
                override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

                }

                override fun onPageSelected(position: Int) {
                    if (position == bannerData.size - 1){
                        mDataBinding.tvStart.visibility = View.VISIBLE
                    }else{
                        mDataBinding.tvStart.visibility = View.INVISIBLE
                    }
                }

                override fun onPageScrollStateChanged(state: Int) {

                }
            })

            //立即进入按钮的点击事件
            mDataBinding.tvStart.setOnClickListener {
                jumpToMainPage()
            }
        }
        //显示广告
        else{
            mDataBinding.clSplashAdv.visibility = View.VISIBLE
            mCountDownTimer = object : CountDownTimer(3000, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    mDataBinding.tvSplashSkip.text = "${millisUntilFinished / 1000}秒跳过"
                }
                override fun onFinish() {
                    this@SplashActivity.jumpToMainPage()
                }
            }.start()
        }
    }

    private fun jumpToMainPage(){
        ARouter.getInstance().build(RoutePath.PATH_MAIN_NAV_PAGE).navigation()
        this@SplashActivity.finish()
    }

}